package com.lepu.app.main.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.config.AppConfig;
import com.app.utils.SdLocal;
import com.app.utils.Utils;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.ImageUtilBase;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.eyzhs.app.R;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

/*
 * 启动广告页
 */
public class AppStartActivityAd extends BaseFragmentActivity {

    private static final int DELAY_TIMES = 3000;//几秒自动关闭广告

    private Timer mAdTimer = null;
    TextView mFinishTextView;
    private int mTimeValue = 3;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        mNeedSwipeBack = false;
        mNeedStatusBar = false;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_start_activity_ad);

        init();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        // 如果是启动页，屏蔽返回键
    }

    private void init() {

        mFinishTextView = (TextView) findViewById(R.id.finishTextView);

        startAdTimer();

        Bundle data = getIntent().getExtras();
        if(data != null && data.size() > 0)
        {
            String filePath = data.getString("filePath");

            File file = new File(filePath);
            if(file.exists())
            {
                ImageView imageView = (ImageView) findViewById(R.id.imageView);
                imageView.setLayoutParams(new FrameLayout.LayoutParams(
                        FrameLayout.LayoutParams.MATCH_PARENT,
                        FrameLayout.LayoutParams.MATCH_PARENT));
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);

                UniversalImageLoadTool.disPlay(ImageUtilBase.getFileImageUrl(filePath), imageView, 0);

                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        Utils.clickAd(MainTabActivity.getInstance(), 1);
                        close();
                    }
                });
            }
        }
    }

    Handler mHandler = new Handler(){
        public void handleMessage(Message msg){
            switch (msg.what){
                case 0:

                    if(mTimeValue <= 0)
                    {
                        close();
                    }else{
                        mFinishTextView.setText(String.format("跳过 %d", mTimeValue));
                        mTimeValue --;
                    }
                    break;
                default:
                    break;
            }
        }
    };

    private void close()
    {
        finishAnim(0,0);
        stopAdTimer();
    }

//    private void jumpApp()
//    {
//        Intent intent = new Intent(this,MainTabActivity.class);
//        startActivityAnim(intent,0,0);
//    }

    public void viewClick(View v)
    {
        int rid = v.getId();
        switch (rid)
        {
            case R.id.finishTextView:

                close();
                break;
            default:
                break;
        }
    }

    public void startAdTimer() {
        if (mAdTimer == null) {
            mAdTimer = new Timer();
            mAdTimer.schedule(new TimerTask() {

                @Override
                public void run() {
                    //
                    mHandler.sendEmptyMessage(0);
                }
            }, 0, 1000);
        }
    }

    public void stopAdTimer() {
        if (mAdTimer != null) {
            mAdTimer.cancel();
            mAdTimer.purge();
            mAdTimer = null;
        }
    }
}
